package com.wangzhixuan.commons.utils.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sun.misc.BASE64Encoder;

import com.wangzhixuan.commons.utils.UUIDGenerator;
import com.wangzhixuan.commons.utils.date.DateUtil;
import com.wangzhixuan.model.vo.ExcelJobInfomationVo;
import com.wangzhixuan.model.wordVo.WordAnalysisVo;
import com.wangzhixuan.model.wordVo.WordMeetingVo;
import com.wangzhixuan.model.wordVo.WordPeopleVo;
import com.wangzhixuan.model.wordVo.WordSurveyVo;

/**
 * 用XML模板生产WORD
 * @author GTE
 *
 */
public class CreatDoc {
	
	 public static void CreatWord(ExcelJobInfomationVo excelJobInfo,WordSurveyVo wdSurvey
			 ,WordMeetingVo wordMeetingVo,List<WordAnalysisVo> wordAnalysisVo,List<String> wdPhenom,
			 List<WordPeopleVo> wordPeopleVo,List<String> imgPath,String getRootPath,String getbaseUrl) {
	        DocUtil docUtil=new DocUtil();
	        Map<String, Object> dataMap=new HashMap<String, Object>();
	        SimpleDateFormat DatetoString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        dataMap.put("A", excelJobInfo.getJobName());
	        dataMap.put("B", excelJobInfo.getJobDescribe());
	        dataMap.put("C", excelJobInfo.getJobCategory());
	        dataMap.put("D", excelJobInfo.getAddress());
	        dataMap.put("E", excelJobInfo.getStatus());
	        dataMap.put("F", excelJobInfo.getCreatName());
	        dataMap.put("G", DatetoString.format(excelJobInfo.getEndTime()));
	        //dataMap.put("H", excelJobInfo.getCreatPhone());                      
	        dataMap.put("I", DatetoString.format(excelJobInfo.getInfaceEndTime()));
	        dataMap.put("J", DatetoString.format(excelJobInfo.getStartTime()));
	        dataMap.put("K", DatetoString.format(wdSurvey.getSurveyTime()));
	        dataMap.put("L", wdSurvey.getSurveyName());
	        dataMap.put("M", wdSurvey.getSurveyContent());
	        dataMap.put("N", wdSurvey.getBlackoutRange());
	        dataMap.put("O", wdSurvey.getSecurity());
	        dataMap.put("P", wdSurvey.getUserName());
	        if("11".equals(wordMeetingVo.getType())){
	        	if("22".equals(wordMeetingVo.getStatus())){
	        		dataMap.put("Q", "未结束");
	        		dataMap.put("R", "未开始");
	        	}
	        	if("23".equals(wordMeetingVo.getStatus())){
	        		dataMap.put("Q", "已结束");
	        		dataMap.put("R", "已结束");
	        	}
	        	
	        }else if("12".equals(wordMeetingVo.getType())){
	        	if("22".equals(wordMeetingVo.getStatus())){
	        		dataMap.put("Q", "已结束");
	        		dataMap.put("R", "未结束");
	        	}
	        	if("23".equals(wordMeetingVo.getStatus())){
	        		dataMap.put("Q", "已结束");
	        		dataMap.put("R", "已结束");
	        	}
	        }else{
        		dataMap.put("Q", "已结束");
        		dataMap.put("R", "已结束");
	        }
	        String listWdPhenom = "";
	        String Reason = "";
	        String Method = "";
	        String Status = "";
	        String CreatTime= "";
	        String uName = "";
	        for(int i=0;i<wdPhenom.size();i++){
	        	listWdPhenom = listWdPhenom+"【现象"+(i+1)+"】:"+wdPhenom.get(i)+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	Reason = Reason+"【原因"+(i+1)+"】:"+wordAnalysisVo.get(i).getReason()+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	Method = Method+"【方法"+(i+1)+"】:"+wordAnalysisVo.get(i).getMethod()+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	Status = Status+"【问题"+(i+1)+"】:"+wordAnalysisVo.get(i).getStatus()+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	uName = uName+"【负责人"+(i+1)+"】:"+wordAnalysisVo.get(i).getuName()+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	CreatTime = CreatTime+"【日期"+(i+1)+"】:"+DatetoString.format(wordAnalysisVo.get(i).getCreatTime())+";  ";
	        }
	        dataMap.put("S",listWdPhenom);
	        dataMap.put("T",Reason);
	        dataMap.put("U",Method);
	        dataMap.put("V",Status);
	        dataMap.put("W",uName);
	        dataMap.put("X",CreatTime);
	        String pName = "";
	        String pCreatTime= "";
	        String address = "";
	        for(int i=0;i<wordPeopleVo.size();i++){
	        	pName = pName+"【"+(i+1)+"】:"+wordPeopleVo.get(i).getuName()+";  ";
	        }
	        for(int i=0;i<wordPeopleVo.size();i++){
	        	pCreatTime = pCreatTime+"【时间"+(i+1)+"】:"+DatetoString.format(wordPeopleVo.get(i).getCreatTime())+";  ";
	        }
	        for(int i=0;i<wordPeopleVo.size();i++){
	        	address = address+"【地址"+(i+1)+"】:"+wordPeopleVo.get(i).getAddress()+";  ";
	        }
	        dataMap.put("Y",pName);
	        dataMap.put("Z",pCreatTime);
	        dataMap.put("A2",address);
	        if(imgPath.size()>=2){
	        	dataMap.put("image", getImageStr(imgPath.get(0),getRootPath,getbaseUrl));
	 	        dataMap.put("image2", getImageStr(imgPath.get(1),getRootPath,getbaseUrl));
	 	        dataMap.put("image3", getImageStr(imgPath.get(2),getRootPath,getbaseUrl));
	        }
	        Date date = new Date();
	        String resId = UUIDGenerator.getUUID();
	        String filePath = DateUtil.DateToString(date, "YYYY/MM/dd");
	        File f = new File(getRootPath +"myword/"+filePath);
			if (!f.exists()){
				f.mkdirs();
			}
			StringBuffer tempfilePathBuffer = new StringBuffer();
			tempfilePathBuffer.append(getRootPath).append("myword/").
			append(filePath).append("/").append(resId).append(".doc");
			String tempfilePath = tempfilePathBuffer.toString();
			System.out.println(tempfilePath);
			//docUtil.createDoc(dataMap, "gdtask",tempfilePath);
	        docUtil.createDoc(dataMap, "gdtask", "D:\\testWORD_XML.doc");						//本地测试
	    }
	  private static String getImageStr(String imgPath,String getRootPath,String getbaseUrl) {
          //String imgFile = getRootPath+imgPath.replace(getbaseUrl, "");  //服务器
		  String imgFile = ("D:\\suc.png");							//本地测试
          InputStream in = null;
          byte[] data = null;
          try {
              in = new FileInputStream(imgFile);
              data = new byte[in.available()];
              in.read(data);
              in.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
          BASE64Encoder encoder = new BASE64Encoder();
          return encoder.encode(data);
      }
}
